﻿Imports DATA
Imports SERVICIOS

Public Class oFm_asistencia

    Dim oFuncionario As New funcionario_data
    Dim WithEvents oAsistencia As New asistencia_data

    Public _columnas As Integer = 0
    Public _grupoId As Integer = 0
    Public _grupoNombre As String = ""
    Public _Fecha As Date
    Private i As Integer = 0

    Public _eTipo As eTipo
    Public Enum eTipo
        NuevoRegistro = 1
        EditarRegistros
    End Enum

    Private Sub oPb_Salir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oPb_Salir.Click
        Me.Close()
    End Sub

    Private Sub oFm_asistencia_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub

    Private Sub oFm_asistencia_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If _eTipo = eTipo.NuevoRegistro Then
            CrearGrilla()
        Else
            cargaAsistencias(_Fecha, _grupoId)
        End If
    End Sub

    Sub CrearGrilla()
        With oGr_datos
            .Columns.Clear()
            .MaquillaGrilla()
            .SelectionMode = DataGridViewSelectionMode.CellSelect
            .ReadOnly = True
            'agregando columnas de normal y de días 
            .Columns.Add("", "Nombre")
            .Columns.Add("", "Grado")
            .Columns.Add("", "Unidad")
            If _columnas > 0 Then
                For i = 0 To _columnas - 1
                    .Columns.Add("", i + 1)
                    .Columns(.ColumnCount - 1).Width = 40
                Next
            End If
            .Columns.Add("", "Tot.%")
            'Ancho de columnas
            .ColWidth(0) = 250
            .ColWidth(1) = 100
            .ColWidth(2) = 120
            'Color de una columna
            .Columns(0).DefaultCellStyle.BackColor = Color.Aqua
        End With
        'cargamos a los funcionarios
        CargarFuncionarios(_grupoId)
        CalcularPorcentaje()
    End Sub

    Sub crearGrillaDatosExistentes()
        With oGr_datos
            .Columns.Clear()
            .MaquillaGrilla()
            .SelectionMode = DataGridViewSelectionMode.CellSelect
            .ReadOnly = True
            'agregando columnas de normal y de días 
            .Columns.Add("", "Nombre")
            .Columns.Add("", "Grado")
            .Columns.Add("", "Unidad")
            If _columnas > 0 Then
                For i = 0 To _columnas - 1
                    .Columns.Add("", i + 1)
                    .Columns(.ColumnCount - 1).Width = 40
                Next
            End If
            .Columns.Add("", "Tot.%")
            'Ancho de columnas
            .ColWidth(0) = 250
            .ColWidth(1) = 100
            .ColWidth(2) = 120
            'Color de una columna
            .Columns(0).DefaultCellStyle.BackColor = Color.Aqua
        End With
    End Sub

    Sub cargaAsistencias(ByVal fecha As Date, ByVal grupo_id As Integer)
        With oGr_datos
            .Rows.Clear()

            _columnas = oAsistencia.ObtenerCantColumnasxRegistro.Rows(0).Item(0)
            Dim dt As DataTable = oAsistencia.ObtenerNombresFuncionarios(fecha, grupo_id)
            crearGrillaDatosExistentes()
            .RowCount = dt.Rows.Count
            For i As Integer = 0 To dt.Rows.Count - 1
                .TextMatrix(i, 0) = dt.Rows(i).Item("FUN_NOMBRE")
                .TextMatrix(i, 1) = dt.Rows(i).Item("FUN_GRADO")
                .TextMatrix(i, 2) = dt.Rows(i).Item("FUN_UNIDAD")
            Next

            For fila As Integer = 0 To .RowCount - 1
                Dim dtAsis As DataTable = oAsistencia.ObtenerAsistenciasxNombresFuncionarios(Trim(.TextMatrix(fila, 0)))
                Dim y As Integer = 0
                For c As Integer = 3 To .Cols_P - 2
                    Try
                        .TextMatrix(fila, c) = dtAsis.Rows(y).Item("ESTADO")
                        .TagMatrix(fila, c) = dtAsis.Rows(y).Item("ASIS_ID")
                        y += 1
                    Catch ex As Exception

                    End Try
                Next
            Next
            CalcularPorcentaje()
        End With
    End Sub

    Sub CargarFuncionarios(ByVal id_grupo As Integer)
        With oGr_datos
            .Rows.Clear()
            Dim dt As DataTable = oFuncionario.msoBuscarxGrupo(id_grupo)
            Try
                .Rows_P = dt.Rows.Count
                i = 0
                For Each x In dt.Rows
                    .TextMatrix(i, 0) = x.item(0)
                    .TextMatrix(i, 1) = x.item(2)
                    .TextMatrix(i, 2) = x.item(1)
                    i += 1
                Next
            Catch ex As Exception

            End Try
        End With
    End Sub

    Private Sub oGr_datos_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles oGr_datos.CellClick
        With oGr_datos
            If .Col > 2 And .Col < .ColumnCount - 1 Then
                oFm_OpcionesAsistenacia.ShowDialog()
                Select Case oFm_OpcionesAsistenacia.oCb_opciones.SelectedIndex
                    Case 0
                        .TextMatrix(.Row, .Col) = "1"
                    Case 1
                        .TextMatrix(.Row, .Col) = "T"
                    Case 2
                        .TextMatrix(.Row, .Col) = "P"
                    Case 3
                        .TextMatrix(.Row, .Col) = "S"
                    Case 4
                        .TextMatrix(.Row, .Col) = "G"
                    Case 5
                        .TextMatrix(.Row, .Col) = "PA"
                    Case 6
                        .TextMatrix(.Row, .Col) = "COM"
                    Case 7
                        .TextMatrix(.Row, .Col) = "A"
                    Case 8
                        .TextMatrix(.Row, .Col) = "CL"
                End Select
                oFm_OpcionesAsistenacia.Dispose()
                CalcularPorcentaje()
            End If
        End With
    End Sub

    Sub CalcularPorcentaje()
        With oGr_datos
            For n As Integer = 0 To .RowCount - 1
                Dim suma As Integer = 0
                For x As Integer = 3 To .ColumnCount - 1
                    If .TextMatrix(n, x) = "1" Then
                        suma += 1
                    End If
                Next
                .TextMatrix(n, .ColumnCount - 1) = 0
                If suma > 0 Then
                    Dim max As Integer = suma * 100
                    Dim incognita As Integer = max / _columnas
                    .TextMatrix(n, .ColumnCount - 1) = incognita
                End If
            Next
        End With
    End Sub

    Private Sub oPb_guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oPb_guardar.Click
        With oGr_datos
            For i As Integer = 0 To .RowCount - 1
                For x As Integer = 3 To .ColumnCount - 2
                    Dim id As Integer = .TagMatrix(i, x)
                    oAsistencia.minGuarda(id, _Fecha, .TextMatrix(i, 0), .TextMatrix(i, x), _grupoNombre)
                Next
            Next
            MsgBox("Datos guardados correctamente", MsgBoxStyle.Information, Me.Text)
        End With
    End Sub

    Private Sub oAsistencia_ErrorServer(ByVal Descripcion As String) Handles oAsistencia.ErrorServer
        MsgBox(Descripcion, MsgBoxStyle.Exclamation, Me.Text)
    End Sub

    Private Sub oAsistencia_GuardaCorrecto() Handles oAsistencia.GuardaCorrecto

    End Sub
End Class
